package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Title: CookieController
 * @Package: com.project.contoller
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/6/28 - 9:35
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("cookie")
public class CookieController {
    @RequestMapping("add")
    public String addCookie(HttpServletResponse resp){
//        创建cookie对象
        Cookie c1=new Cookie("name","tom");
        Cookie c2=new Cookie("suerId","23");
//        设置过期时间
        c2.setMaxAge(30);

//        向客户端发送cookie信息
        resp.addCookie(c1);
        resp.addCookie(c2);

        return "ok";
    }
@RequestMapping("getCookie")
    public String getCookie(HttpServletRequest req){

//        得到客户端提交的cookie信息
        Cookie[] cookies=req.getCookies();
        String str="";
        for (Cookie c:cookies){
            str+="键"+c.getName()+"值："+c.getValue();
        }
        return str;
    }

}
